import { Service } from 'egg';

interface UpdatePayload {
  user_name?: string;
  avatar?: string;
}

export default class UserService extends Service {
  /**
   * 更新用户基本资料：name / avatar
   */
  public async updateProfile(userId: number | string, payload: UpdatePayload) {
    const { ctx } = this;

    const userModel = (ctx.model as any)?.User;
    if (!userModel) {
      throw new Error('USER_MODEL_NOT_FOUND');
    }

    // 获取并更新
    const user = await userModel.findByPk(userId);
    if (!user) {
      throw new Error('USER_NOT_FOUND');
    }

    Object.assign(user, payload);
    await user.save();
    return {};
  }
}